<?php
session_start();
header("Content-type:text/html;charset=utf-8");
require '../../../php/database_class.php';

$return = [];

function get_str($value, $str)
{
    if ($value == '不限') {
        return '';
    } else {
        if ($value == '未定义' || $value == 'no') {
            return " and (`" . $str . "` = '' or `" . $str . "` is null or `" . $str . "` ='no')";
        } else {
            return " and `" . $str . "` = '" . $value . "'";
        }
    }
}
$arr_column = ['define', 'sort','hidden'];
$query_str = '`id` is not null';
foreach ($arr_column as $i) {
    $query_str .= get_str($_POST['arr'][$i], $i);
}
if (!empty($_POST['arr']['value'])) {
    $query_str .= " and (`name` like '%{$_POST['arr']['value']}%' or `pic_original` like '%{$_POST['arr']['value']}%' or `editor` like '%{$_POST['arr']['value']}%')"; 
}

// $query_str = "`id`<>''";
$db = new dbClass();
$db->getConn();
$db->setTableName("data_pattern");
$resultNum = $db->select("count(*)", $query_str);

$db->setLimit((($_POST['arr']['page_num'] - 1) * 21), 21);
$db->setOrder("`id`", "desc");
$result = $db->select("", $query_str);

$return = [];
$return['page_num'] = $_POST['arr']['page_num'];
$return['page_total'] = ceil($resultNum[0]['count(*)'] / 21);
$return['data_count'] = $resultNum[0]['count(*)'];
foreach ($result as $k) {
    $k['pic'] = $k['pic_file'].'/' . $k['pic'];
    $return['detail'][] = $k;
}

echo json_encode($return);
